package com.cloudsigns.factorymanager2.project.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * Created by tony on 2017/12/14.
 */

public class ViewPageAdapter extends FragmentStatePagerAdapter {
    ArrayList<Fragment> list = new ArrayList<Fragment>();

    private Fragment mCurrentFragment;

    public ViewPageAdapter(FragmentManager fm) {
        super(fm);
    }

    public ViewPageAdapter(FragmentManager fm, ArrayList<Fragment> list) {
        super(fm);
        this.list = list;
    }


    @Override
    public Fragment getItem(int arg0) {
        return list.get(arg0);
    }

    @Override
    public int getCount() {
        return null == list ? 0 : list.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {

        if (mCurrentFragment != object) {
            mCurrentFragment = ((Fragment) object);
        }

        super.setPrimaryItem(container, position, object);
    }

    @Override
    public void notifyDataSetChanged() {
        // TODO Auto-generated method stub
        super.notifyDataSetChanged();
    }

    public Fragment getCurrentFragment() {
        return mCurrentFragment;
    }

    public void updateData(ArrayList<Fragment> list) {
        if (null != list) {
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }

    }

    /**
     * 清理数据
     */
    public void cleanData() {
        if (null != this.list) {
            this.list.clear();
            this.list = null;
        }
    }
}
